

$(function () {

    var customersList;
    $.getJSON("/customer.do", {"limit":10000, "offset":"0"}, function (data) {
        customersList = data.rows;

        $(".selectpicker").each(function () {
            var $this = $(this);
            for (var i in customersList) {
                var cust = customersList[i];
                $this.append('<option value="'
                    + cust.id + '" data-subtext="' + cust.name + '">' + cust.cardNo + '</option>')
            }
            $this.selectpicker('refresh');
        });
    });

    $.getJSON("/strategy.do", function (data) {
       for (var i in data) {
           var row = data[i];
           $("#strategyId").append('<option value="'
               + row.id + '">' + row.scores + '积分 / ' + row.rmb + '元</option>');
       }
    });

    $('#addScore > form').submit(function () {
        $this = $(this);
        $.post('/score.do', $this.serialize(), function (data) {
            var $alert = $('#addScore').find('.alert');
            if(data.code == 0) {
                $alert.removeClass('alert-warning');
                $alert.addClass('alert-success');
                $alert.find('p').text('积分增加成功');
                $alert.show();
            } else {
                $alert.removeClass('alert-success');
                $alert.addClass('alert-warning');
                $alert.find('p').text(data.desc);
                $alert.show();
            }
            $this.context.reset();
            $this.find('.selectpicker').selectpicker('deselectAll');

        }, 'json');

        return false;
    });

    $('#exchange > form').submit(function () {
        $this = $(this);
        $.post('/exchange.do', $this.serialize(), function (data) {
            var $alert = $('#exchange').find('.alert');
            if(data.code == 0) {
                $alert.removeClass('alert-warning');
                $alert.addClass('alert-success');
                $alert.find('p').text('兑换成功');
                $alert.show();
            } else {
                $alert.removeClass('alert-success');
                $alert.addClass('alert-warning');
                $alert.find('p').text(data.desc);
                $alert.show();
            }
            $this.context.reset();
            $this.find('.selectpicker').selectpicker('deselectAll');

        }, 'json');

        return false;
    });
});